package com.jrx.anytxn.param.service.sms.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.sms.PrSmsTemplateTableReq;
import com.jrx.anytxn.param.dto.sms.PrSmsTemplateTableRes;
import com.jrx.anytxn.param.entity.PrSmsTemplateTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrSmsTemplateTableMapper;
import com.jrx.anytxn.param.service.sms.ISmsTemplatesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by lbz
 * @Classname SubjectTableServiceImpl
 * @Description 短信模板实现类
 * @Date 2019/12/5
 */
@Service
@CacheConfig(cacheNames = "SMS_TEMPLATE")
public class SmsTemplatesServiceImpl implements ISmsTemplatesService {

    private static final Logger logger = LoggerFactory.getLogger(SmsTemplatesServiceImpl.class);
    @Resource
    private ExtPrSmsTemplateTableMapper extPrSmsTemplateTableMapper;
    @Autowired
    private ISegmentService segmentService;

    @Override
    public PrSmsTemplateTableRes getByTenIdIdAndTemplateId(String tenantId, String templateId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(templateId)) {
            logger.error("短信模板ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrSmsTemplateTable prSmsTemplateTable = extPrSmsTemplateTableMapper.selectByTenantIdAndTemplateId(tenantId,templateId);
        return BeanMapping.copy(prSmsTemplateTable, PrSmsTemplateTableRes.class);
    }

    @Override
    public PrSmsTemplateTableRes addSmsTemplate(PrSmsTemplateTableReq request) throws TxnException {
        //判断模板是否存在
        PrSmsTemplateTable table = extPrSmsTemplateTableMapper.selectByTemplateIdAndOrgIdAndTenantIdAndValidStatus(request.getTemplateId(),request.getOrganizationId(),request.getTenantId(),null);
        if (table != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrSmsTemplateTable templateTable = BeanMapping.copy(request, PrSmsTemplateTable.class);
        try {
            templateTable.setId(segmentService.getId(Constant.PR_SMS_TEMPLATE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        templateTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrSmsTemplateTableMapper.insertSelective(templateTable);
        return BeanMapping.copy(templateTable, PrSmsTemplateTableRes.class);
    }

    @Override
    public TxnPage<PrSmsTemplateTableRes> findAllPageByCondition(Integer pageNum, Integer pageSize, PrSmsTemplateTable prSmsTemplateTable) throws TxnException {
        Page<PrSmsTemplateTableRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrSmsTemplateTable> tableList = extPrSmsTemplateTableMapper.selectByCondition(prSmsTemplateTable);
        if (tableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrSmsTemplateTableRes> res = BeanMapping.copyList(tableList, PrSmsTemplateTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public List<PrSmsTemplateTableRes> findAllSmsTemplateByTenId(String tenantId) {
        List<PrSmsTemplateTable> prProductTableList = extPrSmsTemplateTableMapper.selectByTenantId(tenantId);
        return BeanMapping.copyList(prProductTableList, PrSmsTemplateTableRes.class);
    }

    @CacheEvict(key = "'smsTemplate_'+#request.getTenantId()+'_'+#request.getTemplateId()")
    @Override
    public PrSmsTemplateTableRes modifySmsTemplateTable(PrSmsTemplateTableReq request) throws TxnBizException {
        PrSmsTemplateTable table = extPrSmsTemplateTableMapper.selectByTenantIdAndTemplateId(request.getTenantId(),request.getTemplateId());
        if (table == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PrSmsTemplateTable prTable = BeanMapping.copy(request, PrSmsTemplateTable.class);
        // 更新
        extPrSmsTemplateTableMapper.updateByTenantIdAndTemplateId(prTable);
        return BeanMapping.copy(prTable, PrSmsTemplateTableRes.class);
    }

    @CacheEvict(key = "'smsTemplate_'+#tenantId+'_'+#templateId")
    @Override
    public Boolean removeByTenantIdAndTemplateId(String tenantId, String templateId) throws TxnBizException {
        if (org.apache.commons.lang.StringUtils.isBlank(tenantId) || org.apache.commons.lang.StringUtils.isBlank(templateId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrSmsTemplateTableMapper.deleteByTenantIdAndTemplateId(tenantId, templateId);
        return i > 0;
    }

    @Override
    public List<PrSmsTemplateTableRes> findAllSmsTemplateByOrgIdAndTenId(String orgId, String tenantId) throws TxnException {
        if (org.apache.commons.lang.StringUtils.isBlank(orgId) || org.apache.commons.lang.StringUtils.isBlank(tenantId)) {
            logger.error("机构号或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY);
        }
        List<PrSmsTemplateTable> tempLatelist = extPrSmsTemplateTableMapper.selectByOrgIdAndTenId(orgId, tenantId);
        return BeanMapping.copyList(tempLatelist, PrSmsTemplateTableRes.class);
    }

    @Cacheable(key = "'smsTemplate_'+#tenantId+'_'+#templateId")
    @Override
    public PrSmsTemplateTable selectByTenantIdAndTemplateId(String tenantId, String templateId){
        PrSmsTemplateTable templateTable = extPrSmsTemplateTableMapper.selectByTenantIdAndTemplateId(tenantId,templateId);
        return templateTable;
    }
}
